package com.qwiki.xml;

import java.io.PrintStream;
import java.util.HashMap;

import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;

public class WikiPageAsyncCategoryCountHandler extends WikiPageAbstractAsyncHandler {
  private WikiXMLPageFilter filter;
  public final Object2IntOpenHashMap<String> counters = new Object2IntOpenHashMap<String>();

  /**
   * @param name
   * @param id
   */
  public WikiPageAsyncCategoryCountHandler(String name, int id) {
    super(name, id);
    counters.defaultReturnValue(0);
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.qwiki.xml.WikiPageAbstractAsyncHandler#doProcess(com.qwiki.xml.WikiXMLPage
   * )
   */
  @Override
  public void doProcess(WikiXMLPage xmlpage) {
    if (filter == null || filter.isGood(xmlpage)) {
      try {
        WikiPage page = xmlpage.parse();
        for(String cat: page.getCategories()){
          counters.put(cat, counters.getInt(cat) + 1);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see com.qwiki.xml.WikiPageHandler#getFilter()
   */
  @Override
  public synchronized WikiXMLPageFilter getFilter() {
    // TODO Auto-generated method stub
    return filter;
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.qwiki.xml.WikiPageHandler#setFilter(com.qwiki.xml.WikiXMLPageFilter)
   */
  @Override
  public synchronized void setFilter(WikiXMLPageFilter filter) {
    this.filter = filter;
  }

}
